package com.macro.mall.tiny.modules.pms.controller;


import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.net.URLEncodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.pms.model.PmsErrorCase;
import com.macro.mall.tiny.modules.pms.model.PmsFactoryData;
import com.macro.mall.tiny.modules.pms.service.PmsFactoryDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2024-10-14
 */
@RestController
@RequestMapping("/pms/pmsFactoryData")
@Api(tags = "PmsFactoryDataController")
@Tag(name = "PmsFactoryDataController", description = "工厂资料管理")
public class PmsFactoryDataController {
    @Autowired
    private PmsFactoryDataService pmsFactoryDataService;


    @ApiOperation("获取工厂资料")
    @GetMapping("/getFactoryData")
    public CommonResult<IPage<PmsFactoryData>> getFactoryData(@ApiParam(defaultValue = "10") @RequestParam Integer pageSize, @ApiParam(defaultValue = "1") @RequestParam Integer pageNum,
                                                              @ApiParam("关键词模糊搜索，分类直接名字") @RequestParam(required = false) String keyword) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 10;
        QueryWrapper<PmsFactoryData> query = new QueryWrapper<PmsFactoryData>();
        if (keyword != null) query.like("name", keyword);
        query.groupBy("name");
        query.orderByDesc("id");
        IPage<PmsFactoryData> result = pmsFactoryDataService.page(new Page<>(pageNum, pageSize, true), query);
        result.getRecords().forEach(e -> e.setUrl(null));
        return CommonResult.success(result);
    }

    @ApiOperation("根据名字获取Html类型工厂资料")
    @GetMapping("/getHtmlFactoryData")
    public CommonResult<List<PmsFactoryData>> getHtmlFactoryData(@ApiParam("总文件名") @RequestParam String name) {
        String decodeName = URLDecoder.decode(name, Charset.defaultCharset());
        List<PmsFactoryData> result = pmsFactoryDataService.list(new QueryWrapper<PmsFactoryData>().eq("name", decodeName).eq("type", 1));
        for (PmsFactoryData pmsFactoryData : result) {
            String[] split = pmsFactoryData.getUrl().split(name + "/");
            String title = split[1].replace(".html", "");
            pmsFactoryData.setTitle(title);
            pmsFactoryData.setUrl(null);
        }
        result.sort(Comparator.comparing(PmsFactoryData::getTitle));
        if (result.isEmpty()) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(result);
    }

    @ApiOperation("获取工厂资料(万能接口) 支持 name type id showStatus")
    @PostMapping("/page")
    public CommonResult<IPage<PmsFactoryData>> pageDate(@ApiParam(defaultValue = "10") @RequestParam Integer pageSize, @ApiParam(defaultValue = "1") @RequestParam Integer pageNum, @RequestBody PmsFactoryData pmsFactoryData) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 10;
        QueryWrapper<PmsFactoryData> query = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(pmsFactoryData.getName())) query.like("name", pmsFactoryData.getName());
        if (pmsFactoryData.getType() != null) query.eq("type", pmsFactoryData.getType());
        if (pmsFactoryData.getId() != null) query.eq("id", pmsFactoryData.getId());
        if (pmsFactoryData.getShowStatus() != null) query.eq("show_status", pmsFactoryData.getShowStatus());
        query.orderByDesc("id");
        IPage<PmsFactoryData> result = pmsFactoryDataService.page(new Page<>(pageNum, pageSize, true), query);
        return CommonResult.success(result);
    }


    @ApiOperation("添加工厂资料")
    @PostMapping("/add")
    public CommonResult<PmsFactoryData> addFactoryData(@RequestBody PmsFactoryData pmsFactoryData) {
        if (pmsFactoryData.getUrl().endsWith(".pdf")) {
            pmsFactoryData.setType(0);
        } else if (pmsFactoryData.getUrl().endsWith(".html")) {
            pmsFactoryData.setType(1);
        }
        pmsFactoryDataService.save(pmsFactoryData);
        return CommonResult.success(pmsFactoryData);
    }

    @ApiOperation("批量删除工厂资料")
    @DeleteMapping("/delete")
    public CommonResult<Void> deleteFactoryData(@RequestParam("ids") List<Integer> ids) {
        pmsFactoryDataService.removeByIds(ids);
        return CommonResult.success(null);
    }

    @ApiOperation("修改工厂资料")
    @PutMapping("/update")
    public CommonResult<Void> updateFactoryData(@RequestBody PmsFactoryData pmsFactoryData) {
        if (pmsFactoryData.getUrl() != null && pmsFactoryData.getUrl().endsWith(".pdf")) {
            pmsFactoryData.setType(0);
        } else if (pmsFactoryData.getUrl() != null && pmsFactoryData.getUrl().endsWith(".html")) {
            pmsFactoryData.setType(1);
        }
        pmsFactoryDataService.updateById(pmsFactoryData);
        return CommonResult.success(null);
    }


}

